package com.ledim.chat;

import android.text.TextUtils;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import org.json.JSONException;
import org.json.JSONObject;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 聊天室管理器
 * <p/>
 * Created by zhuqiao on 16/6/12.
 */
public class ChatController {

    // 聊天室连接管理器
    private ConnectionManager mConnectionManager;

    public ChatController(ChatCallback callback) {
        mConnectionManager = new ConnectionManager(callback);
    }

    /**
     * 连接聊天室
     */
    public void chatConnect(String server, String roomId) {

        if (TextUtils.isEmpty(server)) {
            return;
        }

        LogUtil.e("chat", "server=" + server);
        mConnectionManager.connect(server, roomId);
    }

    /**
     * 断开连接
     */
    public void disConnect() {
        mConnectionManager.close();
    }

    /**
     * 是否连接
     *
     * @return
     */
    public boolean isConnect() {
        return mConnectionManager.isConnect();
    }

    /**
     * 关闭聊天室
     */
    public void close() {
        mConnectionManager.close();
    }

    /**
     * 返回vkey
     *
     * @return
     */
    public String getVkey() {
        String vkey = null;
        if (mConnectionManager != null) {
            vkey = mConnectionManager.getVtKey();
        }
        return vkey;
    }

    /**
     * 设置vkey
     *
     * @param vkey
     */
    public void setVkey(String vkey) {
        if (mConnectionManager != null) {
            mConnectionManager.setVkey(vkey);
        }
    }

    /**
     * @param roomId
     * @param type
     * @param message
     * @param coin
     * @param observer
     *         发送消息
     */
    public void sendRoomMsg(String roomId, final int type, String message, final int coin,
                            LedimHttpObserver observer) {
        if (type != ConstantUtil.ChatType.icon) {
            //图片不进行处理
            message = message.replaceAll("<", "＜").replaceAll(">", "＞");
        }
        final JSONObject json = new JSONObject();
        try {
            json.put("type", type);
            json.put("uid", LedimSpUtils.getUid());
            json.put("timestamp", System.currentTimeMillis());
            if (type == ConstantUtil.ChatType.coin) {
                json.put("coin", coin);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LogUtil.e("ccx msg message==" + json.toString());
        LedimApiServiceFactory.createService(ApiService.class)
                              .sendRoomMsg(roomId, message, getVkey(), json.toString())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(observer);
    }
}
